package com.friddle.Download.PageGet;

import java.util.*;

/**
 * Created by friddle on 12/30/14. do not change in run
 */

public class GetConfigure {
	private Map<String, String> defaultCookies = new LinkedHashMap<String, String>();
	private String charset;
	private int sleepTime = 5000;
	private int retryTimes = 1;
	private int cycleRetryTimes = 0;
	private int timeOut = 5000;
	private static final Set<Integer> DEFAULT_STATUS_CODE_SET = new HashSet<Integer>() {
	};
	private Set<Integer> acceptStatCode = DEFAULT_STATUS_CODE_SET;
	private Map<String, String> headers = new HashMap<String, String>();
	public boolean useGzip = true;
	private String domain = null;
	public String userAgent = null;

	public boolean isUseGzip() {
		return useGzip;
	}

    public GetConfigure()
    {

    }

    public GetConfigure(String domain)
    {
        this.domain=domain;
    }

	public GetConfigure setUseGzip(boolean useGzip) {
		this.useGzip = useGzip;
		return this;
	}

	public GetConfigure addCookie(String name, String value) {
		defaultCookies.put(name, value);
		return this;
	}


	public GetConfigure setDefaultCookies(Map<String, String> defaultCookies) {
		this.defaultCookies = defaultCookies;
		return this;
	}

	/**
	 * set user agent
	 * 
	 * @param userAgent
	 *            userAgent @
	 * @return
	 */
	public GetConfigure setUserAgent(String userAgent) {
		this.userAgent = userAgent;
		return this;
	}

	/**
	 * get cookies
	 * 
	 * @return get cookies
	 */
	public Map<String, String> getCookies() {
		return defaultCookies;

	}

	/**
	 * get cookies of all domains
	 * 
	 * @return get cookies
	 */

	/**
	 * get user agent
	 * 
	 * @return user agent
	 */
	public String getUserAgent() {
		return userAgent;
	}

	/**
	 * get domain
	 * 
	 * @return get domain
	 */
	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain)
	{
		this.domain=domain;
	}



	public GetConfigure setCharset(String charset) {
		this.charset = charset;
		return this;
	}

	/**
	 * get charset set manually
	 * 
	 * @return charset
	 */
	public String getCharset() {
		return charset;
	}

	public int getTimeOut() {
		return timeOut;
	}

	/**
	 * set timeout for downloader in ms
	 * 
	 * @param timeOut
	 * @return
	 */
	public GetConfigure setTimeOut(int timeOut) {
		this.timeOut = timeOut;
		return this;
	}

	/**
	 * Set acceptStatCode.<br>
	 * When status code of http response is in acceptStatCodes, it will be
	 * processed.<br>
	 * {200} by default.<br>
	 * It is not necessarily to be set.<br>
	 * 
	 * @param acceptStatCode
	 * @return @
	 */
	public GetConfigure setAcceptStatCode(Set<Integer> acceptStatCode) {
		this.acceptStatCode = acceptStatCode;
		return this;
	}

	/**
	 * get acceptStatCode
	 * 
	 * @return acceptStatCode
	 */
	public Set<Integer> getAcceptStatCode() {
		if (acceptStatCode.size() == 0) {
			acceptStatCode.add(Integer.valueOf(200));
			acceptStatCode.add(Integer.valueOf(302));
		}
		return acceptStatCode;
	}

	/**
	 * Set the interval between the processing of two pages.<br>
	 * Time unit is micro seconds.<br>
	 * 
	 * @param sleepTime
	 * @return @
	 */
	public GetConfigure setSleepTime(int sleepTime) {
		this.sleepTime = sleepTime;
		return this;
	}

	/**
	 * Get the interval between the processing of two pages.<br>
	 * Time unit is micro seconds.<br>
	 * 
	 * @return the interval between the processing of two pages,
	 */
	public int getSleepTime() {
		return sleepTime;
	}

	public int getRetryTimes() {
		return retryTimes;
	}

	public Map<String, String> getHeaders() {
		return headers;
	}

	public GetConfigure addHeader(String key, String value) {
		headers.put(key, value);
		return this;
	}

	/**
	 * Set retry times when download fail, 0 by default.<br>
	 * 
	 * @return
	 * 
	 *         @
	 */
	public GetConfigure setRetryTimes(int retryTimes) {
		this.retryTimes = retryTimes;
		return this;
	}

	/**
	 * When cycleRetryTimes is more than 0, it will add back to scheduler and
	 * try download again. <br>
	 * 
	 * @return retry times when download fail
	 */
	public int getCycleRetryTimes() {
		return cycleRetryTimes;
	}

	/**
	 * Set cycleRetryTimes times when download fail, 0 by default. <br>
	 * 
	 * @return
	 * 
	 */
	public GetConfigure setCycleRetryTimes(int cycleRetryTimes) {
		this.cycleRetryTimes = cycleRetryTimes;
		return this;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		GetConfigure configure = (GetConfigure) o;
		if (cycleRetryTimes != configure.cycleRetryTimes)
			return false;
		if (retryTimes != configure.retryTimes)
			return false;
		if (sleepTime != configure.sleepTime)
			return false;
		if (timeOut != configure.timeOut)
			return false;
		if (acceptStatCode != null ? !acceptStatCode.equals(configure.acceptStatCode) : configure.acceptStatCode != null)
			return false;
		if (charset != null ? !charset.equals(configure.charset) : configure.charset != null)
			return false;
		if (defaultCookies != null ? !defaultCookies.equals(configure.defaultCookies) : configure.defaultCookies != null)
			return false;
		if (domain != null ? !domain.equals(configure.domain) : configure.domain != null)
			return false;
		if (headers != null ? !headers.equals(configure.headers) : configure.headers != null)
			return false;
		if (userAgent != null ? !userAgent.equals(configure.userAgent) : configure.userAgent != null)
			return false;
		return true;
	}
}
